package sparrowGui.components
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	import sparrowGui.components.item.SButton;
	import sparrowGui.components.item.SRichButton;
	
	/**
	 * 窗体组件
	 * 有拖动,合起展开功能
	 * 
	 * 
		var w:SWindow = new SWindow();
		addChild(w);
	 * 
	 * @author Pelephone
	 * @website http://cnblogs.com/pelephone
	 */
	public class SWindow extends BaseUIComponent
	{
		private static const DRAGBG_NAME:String	= "dragBG";
		private static const FOLDBTN_NAME:String	= "foldSkin";
		private static const CLOSEBTN_NAME:String	= "closeSkin";
		private static const CONTDP_NAME:String	= "contDP";
		private static const SKINBG_NAME:String	= "skinBG";
		
		private var dragBG:DisplayObject;
		private var foldSkin:DisplayObject;
		private var closeSkin:DisplayObject;
		private var skinBG:DisplayObject;
		private var contDP:DisplayObjectContainer;
		
		/**
		 * 合起按钮
		 */
		private var foldBtn:SRichButton;
		private var closeBtn:SButton;
		/**
		 * 构造窗体组件
		 * @param uiVars 皮肤变量
		 */		
		public function SWindow(uiVars:Object=null)
		{
			super(uiVars);
			
			addSkinListen();
		}
		
		override protected function setUI(uiVars:Object=null):void
		{
			super.setUI(uiVars);
			
			var skinDC:DisplayObjectContainer = skin as DisplayObjectContainer;
			if(skinDC)
			{
				dragBG = skinDC.getChildByName(DRAGBG_NAME);
				foldSkin = skinDC.getChildByName(FOLDBTN_NAME);
				closeSkin = skinDC.getChildByName(CLOSEBTN_NAME);
				skinBG = skinDC.getChildByName(SKINBG_NAME);
				contDP = skinDC.getChildByName(CONTDP_NAME) as DisplayObjectContainer;
				
				if(dragBG is Sprite)
					(dragBG as Sprite).buttonMode = true;
				
				foldBtn = new SRichButton(foldSkin);
				closeBtn = new SButton(closeSkin);
			}
		}
		
		override protected function addSkinListen():void
		{
			super.addSkinListen();
			dragBG.addEventListener(MouseEvent.MOUSE_DOWN,onEvt);
			foldSkin.addEventListener(MouseEvent.CLICK,onFoldChange);
			closeSkin.addEventListener(MouseEvent.CLICK,onClose);
		}
		
		override protected function removeSkinListen():void
		{
			super.removeSkinListen();
			dragBG.removeEventListener(MouseEvent.MOUSE_DOWN,onEvt);
			foldSkin.removeEventListener(MouseEvent.CLICK,onFoldChange);
			closeSkin.removeEventListener(MouseEvent.CLICK,onClose);
		}
		
		/**
		 * 关上合起按钮事件
		 * @param e
		 */
		private function onFoldChange(e:Event):void
		{
			if(foldBtn.selected)
			{
				contDP.visible = false;
				skinBG.visible = false;
			}
			else
			{
				contDP.visible = true;
				skinBG.visible = true;
			}
		}
		
		/**
		 * 监听鼠标事件
		 * @param e
		 */
		private function onEvt(e:Event):void
		{
			switch(e.type)
			{
				case MouseEvent.MOUSE_DOWN:
				{
					var gp:Point = skin.globalToLocal(new Point(this.x,this.y));
					var rect:Rectangle = new Rectangle(gp.x,gp.y,(skin.stage.stageWidth-dragBG.height),(skin.stage.stageHeight - 20));
					
					if(!embeSkin)
						skinSP.startDrag(false,rect);
					else
						this.startDrag(false,rect);
					
					skin.stage.addEventListener(MouseEvent.MOUSE_UP,onEvt);
					skin.stage.addEventListener(Event.MOUSE_LEAVE,onEvt);
					break;
				}
				case MouseEvent.MOUSE_UP:
				case Event.MOUSE_LEAVE:
					skinSP.stopDrag();
					this.stopDrag();
					break;
			}
		}
		
		private function onClose(e:Event):void
		{
			if(skin.parent) skin.parent.removeChild(skin);
		}
		
		public function addContChild(child:DisplayObject):DisplayObject
		{
			return contDP.addChild(child);
		}
		
		override public function get height():Number
		{
			if(foldBtn.selected)
				return dragBG.height;
			else
				return super.height;
		}
		
		override public function get width():Number 
		{
			if(foldBtn.selected)
				return dragBG.width;
			else
				return super.width;
		}
		
		private function get skinSP():Sprite
		{
			return skin as Sprite;
		}
		
		override public function getDefaultUIName():String
		{
			return "window";
		}
	}
}